package Polymorphic;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 学生管理程序的界面
 */
public class StudentUI {
    private SuperArray ary = new SuperArray();
    private Scanner sc = new Scanner(System.in);


    public StudentUI() {
        ary.add(new Student("钢铁侠", "男", 20));
        ary.add(new Student("蜘蛛侠", "女", 25));
        ary.add(new Student("喜羊羊", "男", 30));
        ary.add(new Student("佩奇", "女", 30));
        ary.add(new Student("乔治", "男", 30));
        menu();
    }

    /**
     * 菜单界面
     */
    public void menu() {
        System.out.println("欢迎使用学生管理程序");
        while (true) {
            int input = 0;
            try {
                System.out.println("请选择：1-查看所有学生 2-增加 3-修改 4-删除 5-退出 6-搜索");

                input = sc.nextInt();

            } catch (InputMismatchException e) {
                System.out.println("输入类型不匹配");
                sc.next();//清空位置输入的input
                continue;
            }
            if (input == 1) {
                show();
            } else if (input == 2) {
                add();
            } else if (input == 3) {
                update();
            } else if (input == 4) {
                remove();
            } else if (input == 5) {
                System.out.println("准备退出");
                System.exit(0);
            } else if (input == 6) {
                search();
            }
        }
    }

    /**
     * 显示所有学生
     */
    public void show() {
        for (int i = 0; i < ary.size(); i++) {
            Student student = (Student) ary.get(i);
            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
        }
    }

    /**
     *
     * 增加学生
     */

    public void add() {
        System.out.println("请输入学生" + "姓名" + "性别" + "年龄");
        System.out.println("姓名");
        String name = sc.next();
        Pattern pattern = Pattern.compile("(^[\u4e00-\u9fa5]{2,}$)");
        Matcher m = pattern.matcher(name);
        if (m.matches()) {
            System.out.println("性别");
            String denger = sc.next();
            Pattern pattern1 = Pattern.compile("(^[[男]|[女]]{1}+$)");
            Matcher m1 = pattern1.matcher(denger);
            if (m1.matches()) {
                System.out.println("年龄");
                int age = 0;
                try {
                    age = sc.nextInt();

                } catch (InputMismatchException e) {
                    System.out.println("输入年龄类型错误");
                    sc.next();
                    return;
                }
                Pattern pattern2 = Pattern.compile("(^[17<=30]{1}+$)");
                String string = age + "";
                Matcher m2 = pattern2.matcher(string);
                if (m2.matches()) {
                    Student student1 = new Student();
                    student1.setName(name);
                    student1.setGender(denger);
                    student1.setAge(age);
                    ary.add(student1);
                    System.out.println("添加成功");
                    System.out.println(student1.getName() + "," + student1.getGender() + "," + student1.getAge());

                } else {

                    System.out.println("输入不合法");
                    return;
                }
            } else {
                System.out.println("输入不合法");
                return;
            }
        } else {
            System.out.println("输入不合法");
            return;
        }
    }

    /**
     * 修改学生
     */
    public void update() {

        System.out.println("请输入姓名");
        String name = sc.next();
        Pattern pattern = Pattern.compile("(^[\u4e00-\u9fa5]{2,}$)");
        Matcher m = pattern.matcher(name);
        if (m.matches()) {
            boolean flag = false;
            for (int i = 0; i < ary.size(); i++) {
                Student student = (Student) ary.get(i);
                if (name.equals(student.getName())) {
                    while (true) {
                        System.out.println("请选择修改的内容  1:姓名, 2:性别 ,3:年龄");
                        int a = 0;
                        try {
                            a = sc.nextInt();
                        } catch (InputMismatchException e) {
                            System.out.println("输入类型错误");
                            sc.next();
                            return;
                        }
                        if (a == 1) {
                            System.out.println("请输入修改的姓名");
                            String name1 = sc.next();
                            Pattern pattern1 = Pattern.compile("(^[\u4e00-\u9fa5]{2,}$)");
                            Matcher m1 = pattern1.matcher(name);
                            if (m.matches()) {

                                student.setName(name1);
                                System.out.println("修改成功是否还要修改");
                                String sr = sc.next();
                                if (sr == "是") {
                                    break;
                                }
                            } else if (a == 2) {
                                System.out.println("请输入修改的性别");
                                String denger1 = sc.next();
                                Pattern pattern2 = Pattern.compile("^[[男]|[女]]{1}+$");
                                Matcher m2 = pattern2.matcher(name);
                                if (m2.matches()) {
                                    student.setGender(denger1);
                                    System.out.println("性别修改成功");
                                    String sr = sc.next();
                                    if (sr == "是") {
                                        break;
                                    } else if (a == 3) {
                                        System.out.println("请输入修改的年龄");
                                        int age1 = sc.nextInt();

                                        Pattern pattern3 = Pattern.compile("(^[17<=30]{1}+$)");
                                        String string = age1 + "";
                                        Matcher m3 = pattern2.matcher(string);
                                        if (m3.matches()) {
                                            student.setAge(age1);
                                            System.out.println("年龄修改成功是否还有修改");
                                            String sr1 = sc.next();
                                            if (sr1 == "是") {
                                                break;
                                            }
                                            if (sr == "否" || sr1 == "否") {
                                                return;
                                            }
                                        }
                                        System.out.println("成功");
                                        return;
                                    }
                                }
                            }
                        }
                        System.out.println("没有这个人");
                        return;
                    }
                }
            }
        }else {
            System.out.println("不合法");
        }
    }

    /**
     * 删除学生
     */
    public void remove() {
        System.out.println("请输入要删除的同学");
        String name = sc.next();

        for (int i = 0; i < ary.size(); i++) {
            Student student = (Student) ary.get(i);
            if (name.equals(student.getName())) {
                ary.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("对不起没有找到" + name);

    }

    /**
     * 搜索
     */
    public void search() {
        while (true) {
            System.out.println("输入你要搜索的方法：1,姓名   2,性别   3,年龄");
            int num = 0;
            try {
                num = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入类型不匹配");
                sc.next();
                continue;
            }

            if (num == 1) {

                System.out.println("输入姓名");
                String name = sc.next();
                        for (int i = 0; i < ary.size(); i++) {
                            int index = 0;
                            Student student1 = (Student) ary.get(i);
                            System . out . println( student1.getName() . matches( "^name[u4e00-u9fa5]{1}$"));
                            boolean flag = true;
                            if (name.equals(student1.getName())) {
                                index++;
                                flag = true;
                                return;
                            }
                            if (index == 0) {
                                System.out.println("没有找到这人");
                                return;
                            }

                        }


                    } else if (num == 2) {
                        System.out.println("输入性别");
                        String denger = sc.next();
                        int index = 0;
                        for (int i = 0; i < ary.size(); i++) {
                            Student student = (Student) ary.get(i);
                            boolean flag = true;
                            if (denger.equals(student.getGender())) {
                                System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                                index++;
                                flag = true;
                            }
                            if (index == 0) {
                                System.out.println("没有这个性别");
                                return;
                            }

                        }
// 可以输入死循环

                    } else if (num == 3) {
                        System.out.println("输入年龄");
                        int age = 0;
                        try {
                            age = sc.nextInt();  //break:
                        } catch (InputMismatchException e) {
                            System.out.println("输入类型不符");
                            sc.next();
                            return;
                        }
                        boolean flag = true;
                        int a = 0;
                        for (int i = 0; i < ary.size(); i++) {
                            Student student1 = (Student) ary.get(i);
                            if (age == student1.getAge()) {
                                System.out.println(student1.getName() + "," + student1.getGender() + "," + student1.getAge());
                                a++;
                                flag = true;
                                return;
                            }
                            if (flag == false) {
                                if (a == 0) {
                                    System.out.println("输入错误");
                                    return;
                                }

                            }
                        }
                    }
                }
            }


    public static void main(String[] args) {
        new StudentUI();
    }
}